<!-- 收银台 -->
<template>
	<view class="pay-method-wrap">
		<shopro-login-modal></shopro-login-modal>
		<view style="height: 286rpx; background: #f3f3f3;">
			<view class="u-flex-col u-col-center money-box" v-if="orderDetail.order_pay">
				<view class="money">{{ orderDetail.order_pay ? orderDetail.order_pay.amount : 0 }}</view>
				<text class="time" v-show="isCountDown">{{ timeText }}</text>
				<!-- <view>{{intent}}</view>
				<view>{{intent2}}</view> -->
			</view>
		</view>
		<!-- <view style="color: #c4c4c4;text-align: center;">扣除乐砖：{{orderDetail.amount_discount}}</view> -->
		<!-- 支付方式单选项 -->
		<u-radio-group  class="pay-box" v-model="payType" active-color="#f0c785">
			
			<!-- 余额支付 -->
			<view class="u-flex u-row-between pay-item" v-show="payment.includes('wallet') && buttons['3']" @tap="payType = 'wallet'">
				<view class="u-flex">
					<image class="pay-img" src="../../../static/images/index/licai.png" mode=""></image>
					<text>余额支付（可用余额￥{{userInfo.credit2}}）</text>
				</view>
				<u-radio shape="circle" name="wallet"></u-radio>
			</view>
			
			<!-- 微信支付 -->
			<view class="u-flex u-row-between pay-item" v-show="payment.includes('wechat') && buttons['9']" @tap="payType = 'wechat'">
				<view class="u-flex">
					<image class="pay-img" src="@/static/images/index/wx.png" mode=""></image>
					<text>微信支付</text>
				</view>
				<u-radio shape="circle" name="wechat"></u-radio>
			</view>
			
			<!-- 微信支付 -->
			<view class="u-flex u-row-between pay-item" v-show="payment.includes('wechat') && buttons['24']" @tap="payType = 'wechat'">
				<view class="u-flex">
					<image class="pay-img" src="@/static/images/index/wx.png" mode=""></image>
					<text>微信支付</text>
				</view>
				<u-radio shape="circle" name="wechat"></u-radio>
			</view>
			<!-- 支付宝支付 -->
			<!-- #ifndef MP-WEIXIN -->
			<view class="u-flex u-row-between pay-item" v-show="payment.includes('alipay')" @tap="payType = 'alipay'">
				<view class="u-flex">
					<image class="pay-img" src="@/static/images/order/order_ali_pay.png" mode=""></image>
					<text>支付宝支付</text>
				</view>
				<u-radio shape="circle" name="alipay"></u-radio>
			</view>
			<!-- #endif -->

			<!-- 苹果支付 -->
			<!-- <view class="u-flex u-row-between pay-item" v-show="payment.includes('iospay') && appPlatfrom === 'ios'" @tap="payType = 'iospay'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_apple_pay.png'" mode=""></image>
					<text>ApplePay</text>
				</view>
				<u-radio shape="circle" name="iospay"></u-radio>
			</view> -->
		</u-radio-group>

		<button class="u-reset-button pay-btn" @tap="confirmPay">确认支付
			￥{{ orderDetail.order_pay.orders[0]['order_goods_price'] || '0.00' }}</button>
		<!-- 登录提示 -->
		<!-- <shopro-auth-modal></shopro-auth-modal> -->
		<shopro-login-modal></shopro-login-modal>
		<u-modal v-model="sueessPay" show-confirm-button show-cancel-button content="是否已经完成支付!" @confirm="removeConfirm"
			@cancel="removeCancel" cancel-text="支付遇到问题"></u-modal>
	</view>
</template>

<script>
	import Pay from '@/shop/pay';
	import { mapMutations, mapActions, mapState } from 'vuex';
	let timer;
	export default {
		components: {},
		data() {
			return {
				payType: 'wallet', //支付方式
				payment: ["wechat", "alipay", "wallet", "wechatH5"],
				isCountDown: true, //是否显示订单倒计时。
				orderDetail: {
					order_pay: {
						orders: [{order_goods_price: 0}]
					}
					
				},
				timeText: '', //倒计时文本
				// platform: this.$platform.get(),
				appPlatfrom: '',
				orderPrams: { // 获取订单支付信息
					isfight: 1,
					order_ids: '',
					mid: '',
				},
				// 提交参数,唤起app支付
				orderPayPrams: {

				},
				intent: "",
				sueessPay: false, //是否显示已经完成支付的确认框
				buttons: {}, //控制支付方式显示
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		async onShow() {
			// this.countDown();
		},
		async onLoad() {
			this.getUserPrefile();
			this.orderPrams.order_ids = this.$Route.query.orderId;
			this.orderPrams.mid = this.userInfo.uid;
			await this.getOrderDetail();
			this.countDown();
			// if(this.$platform.get() === 'wxOfficialAccount' && this.$platform.device() === 'ios' && this.$platform.entry() !== location.href) location.reload();
			// this.getOrderDetail();
			// if (this.payment.length) {
			// 	this.payType = this.payment[0];
			// }
		},
		onHide() {
			clearInterval(timer);
			timer = null;
		},
		methods: {
			...mapActions(['getUserPrefile']),
			// 完成支付
			removeConfirm() {
				this.$router.replace({
					path: "/pages/order/list"
					// query: parmas
				})
			},
			// 支付遇到问题
			removeCancel() {
				this.sueessPay = false;
			},
			// 倒计时
			countDown() {
				let that = this;
				
				let t = (this.orderDetail.pay_end) * 1000 - parseInt(new Date().getTime());
				t = t / 1000;
				timer = setInterval(() => {
					if (t > 0) {
						let time = that.$tools.format(t);
						that.timeText = `支付剩余时间 ${time.m}:${time.s}`;
						t--;
					} else {
						clearInterval(timer);
						that.timeText = '订单已过期!';
					}
				}, 1000);
			},

			// 发起支付
			confirmPay() {
				// this.intent = '202108061006' + '\n' +  this.orderDetail.amount_receivable
				// + '\n' + this.orderDetail.no + '\n' + this.orderDetail.amount_discount ;
				// this.intent2 = this.orderDetail.leid;
				// this.sueessPay = true;
				// window.android.pay('202108061006', this.orderDetail.amount_receivable, this.orderDetail.no, this.orderDetail.amount_discount, this.orderDetail.leid);
				let that = this;
				let pay = null;
				if (!that.payType) {
					this.$u.toast('请选择支付方式');
					return;
				}
				if(this.timeText == '订单已过期!'){
					this.$u.toast('订单过期，请重新下单!');
					return;
				}
				
				that.orderDetail.goodsId = this.$Route.query.goodsId;
				pay = new Pay(that.payType, that.orderDetail);
				// uni.redirectTo({
				// 	url: "/pages/order/payment/result?orderId=1295&type=wallet&payState=success"
				// })
			},

			// 第四步支付信息
			getOrderDetail() {
				// uni.removeStorageSync('mid');
				let share_uid = uni.getStorageSync('shareid');
				let that = this;
				if(share_uid){
					this.orderPrams.share_uid = share_uid;
				}
				this.orderPayPrams.mid = this.userInfo.uid
				this.orderPayPrams.order_ids = this.$Route.query.orderId
				// uni.showToast({
				// 	title: this.orderPrams.share_uid,
				// 	icon: "none"
				// })
				return this.$api.post('/addons/yun_shop/api.php?route=order.merge-pay', this.orderPrams).then(res => {
					this.orderDetail = res.data; // 页面展示
					this.orderDetail.isfight = this.$Route.query.isfight;
					this.buttons = res.data.buttons;
				})
			}
		}
	};
</script>

<style scoped lang="scss">
	.money-box {
		background: #f3f3f3;
		height: 250rpx;
		padding-top: 89rpx;

		.time {
			font-size: 28rpx;
			color: #c4c4c4;
		}

		.money {
			font-size: 50rpx;
			font-weight: bold;
			color: #FF6027;

			&::before {
				content: '￥';
				font-size: 46rpx;
			}
		}
	}

	.pay-box {
		.pay-item {
			height: 90rpx;
			padding: 0 30rpx;
			font-size: 26rpx;
			background: #fff;
			width: 750rpx;
			border-bottom: 1rpx solid #eeeeee;

			&:last-child {
				border-bottom: none;
			}


		}
	}

	.pay-btn {
		position: fixed;
		bottom: 30rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 690rpx;
		line-height: 80rpx;
		background: linear-gradient(98deg, #E95E2A, #F0953E);
		border-radius: 40px;
		color: rgba(#fff, 0.9);
		margin: 100rpx auto 0;
	}

	.pay-img {
		width: 40upx;
		height: 40upx;
		margin-right: 25rpx;

	}
</style>
